#! /bin/bash
# this script is for generate accx rpm package

usrName=`whoami`
if [ $usrName = "root" ];then
        echo "ERROR: please use not root user execute this shell"
        exit 0
fi

targetName=`sed -n '/Name:/p' ./accx.spec |grep -v "#" |awk '{printf $2}'`
targetVersion=`sed -n '/Version:/p' ./accx.spec | grep -v "#" | awk '{printf $2}'`
targetRelease=`sed -n '/Release:/p' ./accx.spec | grep -v "#" | awk '{printf $2}'`
targetBuildTime=`date "+%Y-%m-%d %H:%M:%S"`
targetBuilder=`whoami`
targetSvnVer=`svn info ./generateInfo.sh | grep Revision | awk '{printf $2}'`
targetArch=`uname -m`
targetRpmPostfix=$targetArch.rpm
targetLoadName=$targetName-$targetVersion-$targetRelease-$targetSvnVer.$targetRpmPostfix
targetDebugLoadName=$targetName-debuginfo-$targetVersion-$targetRelease-$targetSvnVer.$targetRpmPostfix

proName=`basename $0`
proPath=`dirname $0`
srcPath=`readlink -f ${proPath}/../`
currentPath=`readlink -f ${proPath}`
tarPath=`readlink -f ${proPath}/../../build`
arch=`uname -m`

#loadName
loadName=$targetName-$targetVersion-$targetRelease.$targetRpmPostfix
debugLoadName=`cat $currentPath/loadInfo.txt |grep debugLoad | awk '{printf $3}'`

echo "proName=$proName"
echo "proPath=$proPath"
echo "srcPath=$srcPath"
echo "currentPath=$currentPath"
echo "loadName=$loadName"
echo "tarPath=$tarPath"

#rpmbuild path home/${usrName}
rpmSourcePath=/home/${usrName}/rpmbuild/SOURCES
rpmRpmsPath=/home/${usrName}/rpmbuild/RPMS
packetPath=/home/${usrName}/rpmbuild/RPMS/${arch}

mkdir -p $tarPath
mkdir -p $rpmSourcePath
mkdir -p $rpmRpmsPath
mkdir -p $packetPath

#clear the rpm RPMS
if [ -s $packetPath/$loadName ];then
    rm $packetPath/$loadName
else
	echo "$packetPath/$loadName not exist"
	#exit -1
fi

#clear the rpm SOURCE
rm $rpmSourcePath/accx.tar.gz

#copy the loadInfo.txt to Dos
cp $currentPath/loadInfo.txt $tarPath

#copy config to Dos
cp $currentPath/../config -rf $tarPath


#copy go bin
cp $srcPath/accx  -f $tarPath/
#tar bin lib loadInfo.txt
cd $tarPath
tar zcf accx.tar.gz   accx config loadInfo.txt
rm loadInfo.txt

#mv cmAgent.tar.gz to SOURCES
mv $tarPath/accx.tar.gz $rpmSourcePath
ls -lrt $rpmSourcePath

#rpmbuild the upf rpm package
rpmbuild -bb $currentPath/accx.spec

#copy the rpm packet to ...Dos/rpmPack
cd ${rpmRpmsPath}
cd ${packetPath}
if [ -s $loadName ];then
   rm ${tarPath}/rpmPack/*.rpm >/dev/null 2>&1
   mkdir -p ${tarPath}/rpmPack
   cp $loadName ${tarPath}/rpmPack/$targetLoadName

   echo "copy path"${packetPath}
   #cp $debugLoadName ${tarPath}/rpmPack/
   cd ${tarPath}/rpmPack
   echo "=========================================================================================="
   echo "general the RPM package at "${tarPath}/rpmPack/$targetloadName
   echo "=========================================================================================="
fi  

exit 0
